/*
 * @Author: qq417045639
 * @Date: 2022-03-04 21:18:54
 * @LastEditors: qq417045639
 * @LastEditTime: 2022-03-26 09:50:20
 * @FilePath: /lib/uniCloud.js
 * @Description: In User Settings Edit
 */

/*
 需配置项
*/


const isDebug = true //是否开启调试模式 调试会在控制台输出内容
/*
 无须修改
*/
function debugPrint(data) {

	if (isDebug) {
		console.log(data);
	}
}
/**
 * 数据库类
 */
class UD {
	constructor(tableName) {
		if (!tableName) {
			console.error('未指定数据库表!')
			throw exception
		}
		this.tableName = tableName
		this.db = uniCloud.database()
		this.where={}
		this.collection = this.db.collection(this.tableName)
		return this;
	}
	where(where = {}){
		this.where=where
		return this
	}
	
	/**
	 * 查找
	 */
	find() {
		return new Promise((success,err)=>{
			this.collection.where(this.where).get().then(function(res) {
				debugPrint(res.result.data)
				success(res.result.data) 
				
			})
		})
	}
	/**
	 * 添加
	 */
	add(data) {
		return new Promise((success,err)=>{
			try {
				this.collection.add(data).then(function(res) {
					success(res);
				})
			} catch (err) {
				debugPrint(err);
			}
		})
	}
	/**
	 * 删除
	 */
	del(){
		this.collection.where(this.where).remove({'name':'mary'})
	}
	
}

export default UD
